package com.mike.spring.Basic;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class MainClass {
    public static void main(String[] args) {
//        MyXmlApplicationContext context = new MyXmlApplicationContext("/com/mike/spring/Basic/beans.xml");
//        BasicA basicA = (BasicA) context.getBean("basicA");
//        basicA.setName("123");
//        System.out.println("args = [" + basicA + "]");

//        ApplicationContext context = new ClassPathXmlApplicationContext("/com/mike/spring/Basic/beans.xml");
//        BasicA basicA = (BasicA) context.getBean("basicA");
//        basicA.setName("234");
//        System.out.println("args = [" + basicA + "]");

//        MyFileApplicationContext context = new MyFileApplicationContext("a");
//        BasicA basicA = (BasicA) context.getBean("basicA");
//        basicA.setName("123");
//        System.out.println("args = [" + basicA + "]");

//        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
//        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(factory);
//        beanDefinitionReader.loadBeanDefinitions("/com/mike/spring/Basic/beans.xml");
//        factory.addBeanPostProcessor(new MyBeanPostProcessor());
//        BeanFactoryPostProcessor beanFactoryPostProcessor = new MyBeanFactoryPostProcessor();
//        beanFactoryPostProcessor.postProcessBeanFactory(factory);
//        BasicA basicA = (BasicA) factory.getBean("basicA");


        DefaultListableBeanFactory content = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(content);
        beanDefinitionReader.loadBeanDefinitions("/com/mike/spring/Basic/beans3.xml");
//        ApplicationContext content = new ClassPathXmlApplicationContext("/com/mike/spring/Basic/beans3.xml");
//        BasicC basicC = (BasicC) content.getBean("basicC");
        content.addBeanPostProcessor((CommonAnnotationBeanPostProcessor)content.getBean("commonProcessor"));
        BasicC basicC =(BasicC)content.getBean("basicC");
        basicC.getBasicD().basicDTest();
        System.out.println(basicC);

//        DefaultListableBeanFactory content = new DefaultListableBeanFactory();
//        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(content);
//        beanDefinitionReader.loadBeanDefinitions("/com/mike/spring/Basic/beans4.xml");
//        content.addBeanPostProcessor((CommonAnnotationBeanPostProcessor)content.getBean("commonProcessor"));
//        BasicE basicE = (BasicE)content.getBean("basicE");
//        System.out.println(JSON.toJSONString(basicE));

        /**
         * 注意 Spring提供的@Autowired加上@Qualifier和JSR250提供的@Resource等注解属于两个 派系。
         * 如果要实现依赖注入的话，使用一个派别的注解就可以了。
         * 当然，既然<context:anno- tation-config>对两个派系都提供了BeanPostProcessor的支持，混合使用也是没有问题的， 只要别造成使用上的混乱就行。
         */

//        ApplicationContext context = new ClassPathXmlApplicationContext("/com/mike/spring/Basic/beans5.xml");
//        BasicH basicH = (BasicH)context.getBean("basicH");
//        basicH.getBasicG().basicGTest();

    }
}
